﻿using System.Linq;
using System.Web.Mvc;
using System.Web;
using KarReq.Domain.Abstract;
using KarReq.Domain.Entities;
using KarReq.WebUI.Models;
using KarReq.WebUI.Infrastructure;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace KarReq.WebUI.Controllers
{
    [Authorize]
    public class QueueController : KarReqController
    {
        public QueueController(IKaraokeTrackRepository repo) : base(repo) { }

        public ViewResult Index(string returnUrl)
        {
            QueueIndexViewModel viewModel = new QueueIndexViewModel
            {
                Requests = repository.Users.First(u => u.UserID == user.UserID).Requests,
                ReturnUrl = returnUrl
            };

            foreach (Request request in viewModel.Requests)
            {
                request.KaraokeTrack = repository.KaraokeTracks.First(k =>
                    k.KaraokeTrackID == request.KaraokeTrackID);
            }

            return View(viewModel);
        }

        public RedirectToRouteResult AddToQueue(int karaokeTrackId, string returnUrl)
        {
            KaraokeTrack karaokeTrack = repository.KaraokeTracks
                .FirstOrDefault(k => k.KaraokeTrackID == karaokeTrackId);

            Request request = new Request
            {
                RequestTime = DateTime.Now,
                UserID = user.UserID,
                KaraokeTrackID = karaokeTrackId,
                KaraokeTrack = karaokeTrack
            };

            repository.Users.First(u => u.UserID == user.UserID).Requests.Add(request);
            repository.KaraokeTracks.First(k => k.KaraokeTrackID == karaokeTrackId)
                .Requests.Add(request);

            if (karaokeTrack != null)
            {
                repository.SaveRequest(request);
            }

            return RedirectToAction("Index", new { returnUrl });
        }

        public RedirectToRouteResult RemoveFromQueue(int karaokeTrackId, string returnUrl)
        {
            KaraokeTrack karaokeTrack = repository.KaraokeTracks
                .FirstOrDefault(p => p.KaraokeTrackID == karaokeTrackId);

            Request request = repository.Users.First(u => u.UserID == user.UserID)
                .Requests.FirstOrDefault(r => r.KaraokeTrackID == karaokeTrackId); 

            if (karaokeTrack != null)
            {
                repository.DeleteRequest(request);
            }

            return RedirectToAction("Index", new { returnUrl });
        }

        public RedirectToRouteResult ClearQueue(string returnUrl)
        {
            repository.ClearRequests(repository.Users.First(u => u.UserID == user.UserID));

            return RedirectToAction("Index", new { returnUrl });
        }

        public RedirectToRouteResult ClearQueueAndLogOff()
        {
            repository.ClearRequests(repository.Users.First(u => u.UserID == user.UserID));

            return RedirectToAction("LogOff", "Account");
        }

        public ViewResult Summary(string returnUrl)
        {
            QueueIndexViewModel viewModel = new QueueIndexViewModel
            {
                Requests = repository.Users.First(u => u.UserID == user.UserID).Requests,
                ReturnUrl = returnUrl
            };

            return View(viewModel);
        }

        public ViewResult Test()
        {
            string[] filePaths = Directory.GetFiles(@"I:\Music Archive\Karaoke\", "*.*",
                SearchOption.AllDirectories);

            List<KaraokeTrack> karaokeTracks = new List<KaraokeTrack>();

            for (int i = 0; i < filePaths.Length; i++)
            {
                string[] fileSplit = Regex.Split(Path.GetFileNameWithoutExtension(
                    filePaths[i]), " - ");
                if (fileSplit.Length == 3)
                {
                    karaokeTracks.Add(new KaraokeTrack
                    {
                        DiscID = fileSplit[0],
                        Artist = fileSplit[1],
                        Song = fileSplit[2]
                    });

                    KaraokeTrack karaokeTrack = repository.KaraokeTracks.FirstOrDefault(
                        k => k.DiscID == fileSplit[0]);

                    if (karaokeTrack == null)
                    {
                        karaokeTrack = new KaraokeTrack();
                        karaokeTrack.DiscID = fileSplit[0];
                    }

                    karaokeTrack.Artist = fileSplit[1];
                    karaokeTrack.Song = fileSplit[2];

                }
            }

            return View(karaokeTracks);
        }

        /*public ViewResult Test2()
        {
            Queue queue2 = repository.Queues.FirstOrDefault();
            KaraokeTrack karaokeTrack  = queue2.Lines.FirstOrDefault().KaraokeTrack;

            return View(karaokeTrack);
        }*/
    }
}